<template>
	<div>
		<el-row :gutter="15">
<el-form ref="elForm" :model="dataForm" :rules="dataRule" size="small" label-width="100px" label-position="right"  :disabled="setting.readonly" >
<template v-if="!loading && formOperates">
			<el-col :span="12"    v-if="judgeShow('code')" >
				<el-form-item  label="供应商编码"   prop="code" >
					<el-input  v-model="dataForm.code" placeholder="系统自动生成" :disabled="true"  clearable  :style='{"width":"100%"}'>

                </el-input>
                </el-form-item>
            </el-col>
			<el-col :span="12"    v-if="judgeShow('name')" >
				<el-form-item  label="供应商名称"   prop="name" >
					<el-input  v-model="dataForm.name" placeholder="请输入"   :disabled="judgeWrite('name')"   clearable  :style='{"width":"100%"}'>

                </el-input>
                </el-form-item>
            </el-col>
			<el-col :span="12"    v-if="judgeShow('creatortime')" >
				<el-form-item  label="创建时间"   prop="creatortime" >
					<jnpf-open-data  v-model="dataForm.creatortime" readonly   :disabled="judgeWrite('creatortime')"   type="currTime" >

                </jnpf-open-data>
                </el-form-item>
            </el-col>
			<el-col :span="12"    v-if="judgeShow('creatoruserid')" >
				<el-form-item  label="创建用户"   prop="creatoruserid" >
					<jnpf-open-data  v-model="dataForm.creatoruserid" readonly   :disabled="judgeWrite('creatoruserid')"   type="currUser" >

                </jnpf-open-data>
                </el-form-item>
            </el-col>
			<el-col :span="12"    v-if="judgeShow('lastmodifyuserid')" >
				<el-form-item  label="修改用户"   prop="lastmodifyuserid" >
					<el-input  v-model="dataForm.lastmodifyuserid" placeholder="系统自动生成"  readonly   :disabled="judgeWrite('lastmodifyuserid')"  >

                </el-input>
                </el-form-item>
            </el-col>
			<el-col :span="12"    v-if="judgeShow('lastmodifytime')" >
				<el-form-item  label="修改时间"   prop="lastmodifytime" >
					<el-input  v-model="dataForm.lastmodifytime" placeholder="系统自动生成"  readonly   :disabled="judgeWrite('lastmodifytime')"  >

                </el-input>
                </el-form-item>
            </el-col>
			<el-col :span="12"    v-if="judgeShow('modifyreason')" >
				<el-form-item  label="修改原因"   prop="modifyreason" >
					<el-input  v-model="dataForm.modifyreason" placeholder="请输入"   :disabled="judgeWrite('modifyreason')"   clearable  :style='{"width":"100%"}'>

                </el-input>
                </el-form-item>
            </el-col>
			<el-col :span="24"    v-if="judgeShow('description')" >
				<el-form-item  label="修改描述"   prop="description" >
					<el-input  v-model="dataForm.description" placeholder="请输入"  readonly   :disabled="judgeWrite('description')"   :style='{"width":"100%"}' true  type="textarea"  :autosize='{"minRows":4,"maxRows":4}' >

                </el-input>
                </el-form-item>
            </el-col>
			<el-col :span="12"    v-if="judgeShow('billstatus')" >
				<el-form-item  label="单据状态"   prop="billstatus" >
					<el-select  v-model="dataForm.billstatus" placeholder="请选择" :disabled="true"  clearable  :style='{"width":"100%"}'>
                            <el-option v-for="(item, index) in billstatusOptions" :key="index" :label="item.fullName" :value="item.id" :disabled="item.disabled" ></el-option>

                </el-select>
                </el-form-item>
            </el-col>
			<el-col :span="12"    v-if="judgeShow('approver')" >
				<el-form-item  label="审批人"   prop="approver" >
					<user-select  v-model="dataForm.approver" placeholder="请选择" :disabled="true"  clearable >

                </user-select>
                </el-form-item>
            </el-col>
			<el-col :span="12"    v-if="judgeShow('approvetime')" >
				<el-form-item  label="审批时间"   prop="approvetime" >
					<el-date-picker  v-model="dataForm.approvetime" placeholder="请选择" :disabled="true"  clearable  :style='{"width":"100%"}' type="datetime"  format="yyyy-MM-dd HH:mm:ss"  value-format="timestamp" >

                </el-date-picker>
                </el-form-item>
            </el-col>
			<el-col :span="12"    v-if="judgeShow('enabledmark')" >
				<el-form-item  label="是否启用"   prop="enabledmark" >
					<el-select  v-model="dataForm.enabledmark" placeholder="请选择"   :disabled="judgeWrite('enabledmark')"   clearable  :style='{"width":"100%"}'>
                            <el-option v-for="(item, index) in enabledmarkOptions" :key="index" :label="item.fullName" :value="item.id" :disabled="item.disabled" ></el-option>

                </el-select>
                </el-form-item>
            </el-col>
			<el-col :span="24"    v-if="judgeShow('attachment')" >
				<el-form-item  label="附件上传"   prop="attachment" >
					<JNPF-UploadFz  v-model="dataForm.attachment" :fileSize="10"     :disabled="judgeWrite('attachment')"   :showTip="true"  sizeUnit="MB"  :limit="9"  buttonText="点击上传" >

                </JNPF-UploadFz>
                </el-form-item>
            </el-col>
			</template>
		</el-form>
		</el-row>
		<UserBox v-if="userBoxVisible" ref="userBox" @submit="submit" />
	</div>
</template>
<script>
	import request from '@/utils/request'
	import { getDataInterfaceRes } from '@/api/systemData/dataInterface'
	import { getDictionaryDataSelector } from '@/api/systemData/dictionary'
	export default {
        components: {},
		props: [],
		data() {
			return {
				visible: false,
				loading: false,
				setting:{},
				eventType: '',
				userBoxVisible:false,
                currTableConf:{},
                addTableConf:{
                },
				tableRows:{
				},
				dataForm: {
					id:'',
					flowId: '',
					status: 1,
					freeapproveruserid: '',
					flowUrgent:1,
										code : undefined,
									name : undefined,
									id : undefined,
										creatortime : undefined,
										creatoruserid : undefined,
										lastmodifyuserid : undefined,
										lastmodifytime : undefined,
									modifyreason : undefined,
									description : undefined,
										billstatus : undefined,
									approver : undefined,
									approvetime : undefined,
										enabledmark : "372347989370343493",
									attachment : [],
            },
			tableRequiredData: {},
				dataRule:
            {




            },
                        billstatusOptions:[],
                        enabledmarkOptions:[],

			}
		},
		computed: {
			formOperates() {
				return this.setting.formOperates
			}
		},
        watch: {},
		created() {
			this.getbillstatusOptions();
			this.getenabledmarkOptions();
        },
		mounted() {},
		methods: {
			judgeShow(id) {
				if (!this.setting.formOperates || !this.setting.formOperates.length) return true
				let arr = this.setting.formOperates.filter(o => o.id === id) || []
				if (!arr.length) return true
				let item = arr[0]
				return item.read
			},
			judgeWrite(id) {
				if (!this.setting.formOperates || !this.setting.formOperates.length) return false
				let arr = this.setting.formOperates.filter(o => o.id === id) || []
				if (!arr.length) return true
				let item = arr[0]
				return !item.write
			},
			getbillstatusOptions() {
				getDictionaryDataSelector('42b0f46122b041d097f1fe4f2f637a03').then(res => {
					this.billstatusOptions = res.data.list
				})
			},
			getenabledmarkOptions() {
				getDictionaryDataSelector('372347785317453893').then(res => {
					this.enabledmarkOptions = res.data.list
				})
			},
                goBack() {
                    this.$emit('refresh')
                },
            init(data) {
                this.dataForm.id = data.id || "";
				this.setting = data;
                this.visible = true;
				this.updateDataRule()
                this.$nextTick(() => {
                    this.$refs['elForm'].resetFields();
                    if(this.dataForm.id){
                        this.loading = true
                        request({
                            url: '/api/example/GySupplier/'+this.dataForm.id,
                            method: 'get'
                        }).then(res => {
                            this.dataInfo(res.data)
                            this.loading = false
                        })
                    }
					this.$emit('setPageLoad')
                })
				this.$store.commit('generator/UPDATE_RELATION_DATA', {})
            },
			updateDataRule() {
				let newRules = {}
				for (let i = 0; i < this.setting.formOperates.length; i++) {
					const item = this.setting.formOperates[i]
					if (!item.id.includes('-')) {
						const newRulesItem = {
							required: item.required,
							message: item.name + '不能为空',
							trigger: item.trigger || 'blur'
						}
						if (!this.dataRule.hasOwnProperty(item.id)) {
							if (item.required) this.$set(newRules, item.id, [newRulesItem])
						} else {
							let withoutRequiredItem = true
							for (let i = 0; i < this.dataRule[item.id].length; i++) {
								if (this.dataRule[item.id][i].hasOwnProperty('required')) {
									this.dataRule[item.id][i].required = item.required
									withoutRequiredItem = false
								}
							}
							if (withoutRequiredItem && item.required) this.dataRule[item.id].push(newRulesItem)
						}
					} else {
						let key = item.id.split('-')[0]
						let newId = item.id.split('-')[1]
						let newItem = {
							...item,
							id: newId
						}
						if (!this.tableRequiredData.hasOwnProperty(key)) {
							this.$set(this.tableRequiredData, key, [newItem])
						} else {
							this.tableRequiredData[key].push(newItem)
						}
					}
				}
				this.dataRule = { ...this.dataRule, ...newRules }
			},
			exist() {
				let isOk = true
				for (let key in this.tableRequiredData) {
					if (this.dataForm[key] && Array.isArray(this.dataForm[key])) {
						for (let i = 0; i < this.dataForm[key].length; i++) {
							let item = this.dataForm[key][i]
							inner: for (let id in item) {
								let arr = this.tableRequiredData[key].filter(o => o.id === id) || []
								if (!arr.length) continue inner
								if (arr[0].required) {
									let msg = `${arr[0].name}不能为空`
									let boo = true
									if (arr[0].dataType === 'array') {
										boo = !this.jnpf.isEmptyArray(item[id])
									} else {
										boo = !this.jnpf.isEmpty(item[id])
									}
									if (!boo) {
										this.$message({
											message: msg,
											type: 'error',
											duration: 1000
										})
										isOk = false
										break
									}
								}
							}
						}
					}
				}
				return isOk
			},
			judgeRequired(id) {
				if (!this.formOperates || !this.formOperates.length) return false
				let arr = this.formOperates.filter(o => o.id === id) || []
				if (!arr.length) return false
				let item = arr[0]
				return item.required
			},
            // 表单提交
            dataFormSubmit(eventType,flowUrgent) {
                this.$refs['elForm'].validate((valid) => {
					if (valid) {
						if (this.exist && !this.exist()) return
						if (eventType === 'audit' || eventType === 'reject') {
							this.$emit('eventReceiver', this.dataForm, eventType)
							return
						}
						this.dataForm.status = eventType === 'submit' ? 0 : 1
						this.dataForm.flowId=this.setting.flowId
						this.dataForm.flowUrgent = flowUrgent
						this.eventType = eventType
						if (this.eventType === 'submit') {
							this.$confirm('您确定要提交当前流程吗, 是否继续?', '提示', {
								type: 'warning'
							}).then(() => {
								this.request()
							}).catch(() => { });
						} else {
							this.request()
						}
					}
                })
            },
            request() {
                var _data =this.dataList()
                if (!this.dataForm.id) {
                    request({
                        url: '/api/example/GySupplier',
                        method: 'post',
                        data: _data
                    }).then((res) => {
                        this.$message({
                            message: res.msg,
                            type: 'success',
                            duration: 1000,
                            onClose: () => {
                                this.visible = false
								this.$emit('close', true)
                            }
                        })
                    })
                }else{
                    request({
                        url: '/api/example/GySupplier/'+this.dataForm.id,
                        method: 'PUT',
                        data: _data
                    }).then((res) => {
                        this.$message({
                            message: res.msg,
                            type: 'success',
                            duration: 1000,
                            onClose: () => {
                                this.visible = false
								this.$emit('close', true)
                            }
                        })
                    })
                }
            },
            openSelectDialog(key) {
                this.currTableConf=this.addTableConf[key]
                this.currVmodel=key
                this.selectDialogVisible = true
                this.$nextTick(() => {
                    this.$refs.selectDialog.init()
                })
            },
            addForSelect(data) {
                for (let i = 0; i < data.length; i++) {
                    let item={...this.tableRows[this.currVmodel],...data[i]}
                    this.dataForm[this.currVmodel].push(item)
                }
            },
            dataList(){
                var _data = JSON.parse(JSON.stringify(this.dataForm));













        _data.attachment = Array.isArray(_data.attachment)? JSON.stringify(_data.attachment):'[]'

                return _data;
            },
            dataInfo(dataAll){
                let _dataAll =dataAll
        _dataAll.attachment = _dataAll.attachment ?JSON.parse(_dataAll.attachment):[]
                this.dataForm = _dataAll
            },
        },
    }

</script>
